package com.health.db.dao.user.impl;

import java.util.ArrayList;
import java.util.List;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.user.IUserCardListDao;
import com.health.db.mapper.UserCardListMapper;
import com.health.db.model.UserCardList;
import com.health.db.model.UserCardListExt;
import com.health.redis.RedisConstants;


public class UserCardListDaoImpl  extends ServiceInterface implements IUserCardListDao{

	//private static SqlSessionFactory factory = SqlSessionFactoryGen	.getSqlSessionFactory();
	UserCardListMapper userCardListMapper;

	String  selectUserCardListByCardId=":cardId:";
	String  selectUserCardListByOwnerId=":ownerId:";
	
/*	@Override
	public int insert(UserCardList record) {
		// TODO Auto-generated method stub
		
		int result=-1;
		result =userCardListMapper.insert(record);
		
		getRedisOperatiorLogic();
		// TODO Auto-generated method stub
		String keyOwnerId=RedisConstants.UserCardListMapper+selectUserCardListByOwnerId+record.getOwnerid();
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteObject(keyOwnerId,getOrderRedisGroupNo("1"));
		}
		
		return result;
	}*/
	
	/**
	 * example object
	 *    {ownerid  用户userId
 	 *     cardid   卡号
	 *     leftTime02
	 *     }
	 */
	@Override
	public List<UserCardList> selectUserCardLists(UserCardListExt example) {

		getRedisOperatiorLogic();
		// TODO Auto-generated method stub
		String key = RedisConstants.UserCardListMapper ;
		
		//卡类型只支撑余额~
		/*if (null==example.getLefttime02()) {
			return null;
		}*/
		if (null!=example.getCardid()&&example.getCardid().intValue()!=0) {
			key=key+selectUserCardListByCardId+ example.getCardid();
		}else{
			key=key+selectUserCardListByOwnerId+example.getOwnerid();
		}
		Object object = null;
		List<UserCardList> list = new ArrayList<UserCardList>();
	
		if (redisOperatiorLogic.isJedisIsConnected()) {
			// get data from redis
			object = redisOperatiorLogic.readObject(key,getPublicRedisGroupNo("1"));
		}

		if (null != object) {
			if ( !object.equals("")) {
				list = (List<UserCardList>) object;
			}
		} else {
		 
			list = userCardListMapper.selectUserCardLists(example);
			if (redisOperatiorLogic.isJedisIsConnected()) {
				redisOperatiorLogic.setEx(key, list,getOrderRedisGroupNo("1"));
			}
		}

		return list;
	}
	

	/**
	 * 
	 * 主键 Cardid
	 * 
	 * record中	包含ownerid
	 * 
	 */
	@Override
	public int updateByPrimaryKeySelective(UserCardList record) {
		// TODO Auto-generated method stub
		
		getRedisOperatiorLogic();
		// TODO Auto-generated method stub
		int result=-1;
		String keyCardId=RedisConstants.UserCardListMapper+selectUserCardListByCardId+record.getCardid();
		String keyOwnerId=RedisConstants.UserCardListMapper+selectUserCardListByOwnerId+record.getOwnerid();
		
		result=userCardListMapper.updateByPrimaryKeySelective(record);
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteObject(keyCardId,getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(keyOwnerId,getOrderRedisGroupNo("1"));
		}
		return result;
	}
	
	/**
	 * 
	 * 主键 Cardid
	 * 
	 * record中	包含ownerid
	 * 
	 */
	@Override
	public void updateByPrimaryKeySelectiveBatch(
			List<UserCardList> userCardLists) {
		// TODO Auto-generated method stub
		userCardListMapper.updateByPrimaryKeySelectiveBatch(userCardLists);
		
		
		getRedisOperatiorLogic();
		// TODO Auto-generated method stub
		int result=-1;
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			
			String keyOwnerId=RedisConstants.UserCardListMapper+selectUserCardListByOwnerId+userCardLists.get(0).getOwnerid();
			redisOperatiorLogic.deleteObject(keyOwnerId,getOrderRedisGroupNo("1"));
			for (int i = 0; i < userCardLists.size(); i++) {
				String keyCardId=RedisConstants.UserCardListMapper+selectUserCardListByCardId+userCardLists.get(i).getCardid();
				redisOperatiorLogic.deleteObject(keyCardId,getOrderRedisGroupNo("1"));
			}
			
		}
	}
	
	@Override
	public int insertNoCardId(UserCardList record) {
		// TODO Auto-generated method stub
		int result=-1;
		result =userCardListMapper.insertNoCardId(record);
		
		getRedisOperatiorLogic();
		// TODO Auto-generated method stub
		String keyOwnerId=RedisConstants.UserCardListMapper+selectUserCardListByOwnerId+record.getOwnerid();
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteObject(keyOwnerId,getOrderRedisGroupNo("1"));
		}
		
		return result;
	}
	
	
	
	

	/*************************************************************************************************************
	/**
	 * @return the userCardListMapper
	 */
	public UserCardListMapper getUserCardListMapper() {
		return userCardListMapper;
	}

	/**
	 * @param userCardListMapper the userCardListMapper to set
	 */
	public void setUserCardListMapper(UserCardListMapper userCardListMapper) {
		this.userCardListMapper = userCardListMapper;
	}



}
